package com.hq.ims.data.service;

import com.hq.ims.data.entity.Area;
import com.hq.ims.data.dao.AreaMapper;
import com.hq.ims.data.vo.AreaMapstruct;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import org.springframework.stereotype.Service;
import com.hq.mybatis.base.BaseService;
import org.springframework.transaction.annotation.Transactional;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.ObjectUtils;
import java.util.List;
import java.util.Map;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author 蒋柏平
 * @since 20241031
 * @detail 区域 服务实现类
 */
@Service
public class BaseAreaService extends BaseService<AreaMapper, Area> {


    /**
    * 分页
    */
    public List<AreaMapstruct.AreaVo> getByPage(
            RequestPage page,
            AreaMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<Area> lq = this.lambdaQuery();
        //查询条件

        //执行分页
        lq.page(page);

        List<Area> dataList = page.getRecords();
        List<AreaMapstruct.AreaVo> voList = AreaMapstruct.INSTANCE.toAreaVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
      * 添加
      */
    @Transactional(rollbackFor = Exception.class)
    public Area addData(AreaMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo,VdAdd.class);

        //账值新的对象
        Area entity = new Area();
        entity.setAreaId(vo.getAreaId());
        entity.setAreaName(vo.getAreaName());
        entity.setParentId(vo.getParentId());

         entity.insert();
         return entity;
    }

     /**
     * 编辑
     */
     @Transactional(rollbackFor = Exception.class)
     public Area editData(AreaMapstruct.SaveVo vo) {
         //验证
        SpringBootValidateUtils.validate(vo,VdEdit.class);

        //账值新的对象
        Area entity = this.getById(vo.getAreaId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setAreaId(vo.getAreaId());
        entity.setAreaName(vo.getAreaName());
        entity.setParentId(vo.getParentId());

        entity.updateById();
        return entity;
     }


    /**
    * 删除
    */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String areaId) {
        Area data = this.getById(areaId);
        if(data == null){
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public AreaMapstruct.AreaVo getDetailVo(String areaId) {
        Area entity = this.getById(areaId);
        AreaMapstruct.AreaVo vo = AreaMapstruct.INSTANCE.toAreaVo(entity);
        return vo;
    }

    /**
     * 根据地区名称和上级id获取地区
     * @param areaName
     * @param parentId
     * @return
     */
    public Area getDetailByNameAndParentId(String areaName,String parentId) {
        return this.lambdaQuery().eq(Area::getAreaName, areaName).eq(Area::getParentId, parentId).one();
    }
}